﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using SMS.Common.Silverlight;
using System.ComponentModel;
using System.Collections.Generic;
using SMS.Entities;
using System.Windows.Data;

namespace SMS.ViewModel.Master
{
    public class SalesManMasterViewModel:BaseNotify
    {
        SMS.Model.SalesManModel _model;
        public SalesManMasterViewModel()
        {
            SearchCommand = new DelegateCommand(Search, null);
            SaveCommand = new DelegateCommand(Save, null);
            NewCommand = new DelegateCommand(New, null);
            CancelCommand = new DelegateCommand(Cancel, null);
            if (!DesignerProperties.IsInDesignTool)
            {
                _model = new Model.SalesManModel();
                _model.OnSearchCompleted += new Action<System.Collections.ObjectModel.ObservableCollection<Entities.SalesManDetail>>(_model_OnSearchCompleted);
                _model.OnSaveSalesManCompleted += new Action<int>(_model_OnSaveSalesManCompleted);
            }
        }

        void _model_OnSearchCompleted(System.Collections.ObjectModel.ObservableCollection<Entities.SalesManDetail> obj)
        {
            UserMessage = obj.Count + " SalesMan found";
            SalesManList = new List<SalesManDetail>(obj);
            PagedSalesManList = new PagedCollectionView(SalesManList);
        }

        void _model_OnSaveSalesManCompleted(int obj)
        {
            UserMessage = "Sales Man saved successfully.";
            SelectedMaster.SalesMan_ID = obj;
        }

        public DelegateCommand SearchCommand { get; set; }
        private void Search(object param)
        {
            _model.SearchSalesMan(SearchText);

            UserMessage = "Searching SalesMan...";
        }

        public DelegateCommand SaveCommand { get; set; }
        private void Save(object param)
        {
            if (SelectedMaster != null)
            {

                _model.SaveSalesMan(SelectedMaster);

                UserMessage = "Saving Sales Man...";
            }
            else
            {
                ErrorMessage = "No SalesMan save";
            }
        }

        public DelegateCommand NewCommand { get; set; }
        private void New(object param)
        {
            if (SalesManList == null)
                SalesManList = new List<SalesManDetail>();
            var k = new SalesManDetail() { DateOfJoining=DateTime.Now,DOB=DateTime.Now, IsLive = true };
            SalesManList.Add(k);
            PagedSalesManList = new PagedCollectionView(SalesManList);
            PagedSalesManList.MoveToLastPage();
            SelectedMaster = k;
        }

        public DelegateCommand CancelCommand { get; set; }
        private void Cancel(object param)
        {
            ViewModel_MainPage.MainPageContext.FrameControl.GoBack();
        }
        private List<SalesManDetail> _SalesManList;

        public List<SalesManDetail> SalesManList
        {
            get { return _SalesManList; }
            set { _SalesManList = value; OnPropertyChanged("SalesManList"); }
        }

        private SalesManDetail _SelectedSalesMan;

        public SalesManDetail SelectedSalesMan
        {
            get { return _SelectedSalesMan; }
            set { _SelectedSalesMan = value; OnPropertyChanged("SelectedSalesMan"); }
        }

        private SalesManDetail _SelectedMaster;

        public SalesManDetail SelectedMaster
        {
            get { return _SelectedMaster; }
            set { _SelectedMaster = value; OnPropertyChanged("SelectedMaster"); }
        }

        private PagedCollectionView _PagedSalesManList;

        public PagedCollectionView PagedSalesManList
        {
            get { return _PagedSalesManList; }
            set { _PagedSalesManList = value; OnPropertyChanged("PagedSalesManList"); }
        }

        private string _SearchText;

        public string SearchText
        {
            get { return _SearchText; }
            set { _SearchText = value; OnPropertyChanged("SearchText"); }
        }

    }
}
